package com.sx.user.controller;

import com.sx.user.entity.Token;
import com.sx.user.entity.User;
import com.sx.user.service.CodeFeignClient;
import com.sx.user.service.RegisterService;
import com.sx.user.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author shengx
 * @date 2020/5/9 23:49
 */
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
    @Value("${test}")
    private String test;
    @Autowired
    private CodeFeignClient codeFeignClient;
    @Autowired
    private RegisterService registerService;

    /**
     * 登录接⼝，验证⽤户名密码合法性，根据⽤户名和密码⽣成token，token存⼊数据库，并写⼊cookie中，登录成功返回邮箱地址，重定向到欢迎⻚
     * @param email
     * @param password
     * @return
     */
    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, HttpServletResponse response) {
        System.out.println("email:" + email + " password:" + password);
        User user = registerService.signIn(email, password);
        if (user != null) {
            String token = TokenUtils.getToken(email);
            Token myToken = registerService.addToken(email, token);
            Cookie cookie = new Cookie("token", myToken.getToken());
            cookie.setPath("/");
            response.addCookie(cookie);
            return email;
        }
        return null;
    }

    /**
     * 根据token查询⽤户登录邮箱接⼝
     * @param token
     * @return
     */
    @GetMapping("/info/{token}")
    public String info(@PathVariable String token) {
        System.out.println("token:" + token);
        Token myToken = registerService.getEmailByToken(token);
        System.out.println(myToken);
        return myToken != null ? myToken.getEmail() : "";
    }

    /**
     * 是否已注册，根据邮箱判断,true代表已经注册过，false代表尚未注册
     * @param email
     * @return
     */
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {
        System.out.println("email:" + email);
        return registerService.isRegister(email);
    }

    /**
     * 注册接⼝，true成功，false失败
     * @param email
     * @param password
     * @param code
     * @return
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public Boolean register(@PathVariable String email, @PathVariable String password, @PathVariable String code) {
        System.out.println("email:" + email + " password:" + password + " code" + code);
        Integer validate = codeFeignClient.validate(email, code);
        int success = -1;
        if (validate == 0) {
            success = registerService.register(email, password);
        }
        if (success > 0) {
            return true;
        }
        return false;
    }

    @GetMapping("/test")
    public String test() {
        return this.test;
    }
}
